/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package me.aviator.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import me.aviator.dao.CommonDao;
import me.aviator.entity.Symbol;
import org.apache.commons.io.FileUtils;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Administrator
 */
public class SymbolService {
    
    private final CommonDao commonDao;
    
    public SymbolService(Session session){
        commonDao=new CommonDao(session);
    }
    
    public List<Symbol> listAll(){
        return commonDao.find(Symbol.class);
    }
     
    public List<Symbol> listByCatagoryA(String groupName){
        Criterion criterion=Restrictions.eq("catagoryA", groupName);
        return commonDao.find(Symbol.class,criterion);
    }
    
    public List<Symbol> listByCatagoryB(String groupName){
        Criterion criterion=Restrictions.eq("catagoryB", groupName);
        return commonDao.find(Symbol.class,criterion);
    }
    
    public List<Symbol> findSymbol(Map<String,?> conditions){
        Criterion criterion=Restrictions.allEq(conditions);
        List<Symbol> symbollist = commonDao.find(Symbol.class, criterion);
		
	if(symbollist.isEmpty()){
            return null;
	}
		
	return symbollist;	
    }
    
    public Symbol findSymbolById(int id){
        Criterion criterion = Restrictions.eq("id", id);
		
        List<Symbol> symbollist = commonDao.find(Symbol.class, criterion);
		
        if(symbollist.size() == 0){
            return null;
        }
		
        return symbollist.get(0);
    }
    
    public void uploadSymbol(String sName,String type,File svgxml,String catagoryA,String catagoryB,int orderID){
        Symbol symbol=new Symbol();
        symbol.setName(sName);
        symbol.setType(type);
        symbol.setCatagoryA(catagoryA);
        symbol.setCatagoryB(catagoryB);
        symbol.setOrderID(orderID);
        String svgXml=null;

        try {
            svgXml = FileUtils.readFileToString(svgxml);
            symbol.setSvgxml(svgXml.getBytes("UTF-8"));
        } catch (IOException ex) {
            Logger.getLogger(SymbolService.class.getName()).log(Level.SEVERE, null, ex);
        }
        commonDao.save(symbol); 
    }
    
    public void addSymbol(Symbol symbol){
        commonDao.save(symbol);
    }
    
    public void removeSymbol(Symbol symbol){
        commonDao.delete(symbol);
    }
    
    public List<Symbol> findRSymbolByName(String rName){
        Criterion criterion = Restrictions.eq("name",rName);
		
	List<Symbol> symbollist = commonDao.find(Symbol.class, criterion);
		
	if(symbollist.size() == 0){
            return null;
	}
		
	return symbollist;
    }
}
